package com.wang.blog.config;

import com.wang.blog.oauth.OAuthRealm;
import com.wang.blog.oauth.OAuthSessionManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 王益波
 * @version 1.0
 * @date 2020/9/11 19:00
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
//       filterChainDefinitionMap.put("/", "anon");
//
//        filterChainDefinitionMap.put("/static/**", "anon");
//        filterChainDefinitionMap.put("/blogFile/**", "anon");
//        filterChainDefinitionMap.put("/login", "anon");
//        filterChainDefinitionMap.put("/register", "anon");
//        filterChainDefinitionMap.put("/**", "authc");

        filterChainDefinitionMap.put("/**/create", "authc");
        filterChainDefinitionMap.put("/**/update", "authc");
        filterChainDefinitionMap.put("/**/delete", "authc");
        filterChainDefinitionMap.put("/**/change", "authc");
        filterChainDefinitionMap.put("/upload", "authc");
        filterChainDefinitionMap.put("/users/currentUser", "authc");

        filterChainDefinitionMap.put("/**", "anon");

        //返回json数据，由前端跳转
        shiroFilterFactoryBean.setLoginUrl("/handleLogin");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public OAuthRealm oAuthRealm() {
        OAuthRealm myShiroRealm = new OAuthRealm();
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myShiroRealm;
    }

    //shiro密码验证配置
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

 //Shiro Session管理器
    @Bean
    public SessionManager sessionManager() {
        OAuthSessionManager oAuthSessionManager = new OAuthSessionManager();
        return oAuthSessionManager;
    }
    @Bean
    public SecurityManager securityManager(OAuthRealm oAuthRealm,SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuthRealm);
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }


}
